package com.lcxw.hxzb.friendcircle.project.system.controller;

import com.lcxw.hxzb.friendcircle.framework.web.controller.BaseController;
import com.lcxw.hxzb.friendcircle.framework.web.domain.AjaxResult;
import com.lcxw.hxzb.friendcircle.framework.web.page.TableDataInfo;
import com.lcxw.hxzb.friendcircle.project.system.domain.YwpdDistrict;
import com.lcxw.hxzb.friendcircle.project.system.domain.YwpdGameDistrict;
import com.lcxw.hxzb.friendcircle.project.system.service.IYwpdDistrictService;
import com.lcxw.hxzb.friendcircle.project.system.service.IYwpdGameDistrictService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 游戏区服Controller
 *
 * @author ruoyi
 * @date 2020-09-15
 */
@RestController
@Api(tags = "游戏区服Controller")
@RequestMapping("/client/game/district")
public class YwpdGameDistrictController extends BaseController {
    @Autowired
    private IYwpdGameDistrictService ywpdGameDistrictService;
    @Autowired
    private IYwpdDistrictService ywpdDistrictService;

    /**
     * 查询游戏区服列表
     */
    @GetMapping("/list")
    public AjaxResult list(YwpdGameDistrict ywpdGameDistrict) {
        List<YwpdGameDistrict> list = ywpdGameDistrictService.selectYwpdGameDistrictList(ywpdGameDistrict);
        Map<Long, List<YwpdDistrict>> districtList = ywpdDistrictService.selectYwpdDistrictList(null).stream().collect(Collectors.groupingBy(YwpdDistrict::getDistrictId));
        for (YwpdGameDistrict district : list) {
            Long districtId = district.getDistrictId();
            YwpdDistrict ywpdDistrict = districtList.get(districtId).get(0);
            district.setYwpdDistrict(ywpdDistrict);
        }
        return AjaxResult.success(list);
    }


    /**
     * 获取游戏区服详细信息
     */
    @GetMapping(value = "/{districtId}")
    public AjaxResult getInfo(@PathVariable("districtId") Long districtId) {
        return AjaxResult.success(ywpdGameDistrictService.selectYwpdGameDistrictById(districtId));
    }

    /**
     * 新增游戏区服
     */
    @PostMapping
    public AjaxResult add(@RequestBody YwpdGameDistrict ywpdGameDistrict) {
        return toAjax(ywpdGameDistrictService.insertYwpdGameDistrict(ywpdGameDistrict));
    }

    /**
     * 修改游戏区服
     */
    @PutMapping
    public AjaxResult edit(@RequestBody YwpdGameDistrict ywpdGameDistrict) {
        return toAjax(ywpdGameDistrictService.updateYwpdGameDistrict(ywpdGameDistrict));
    }

    /**
     * 删除游戏区服
     */
    @DeleteMapping("/{districtIds}")
    public AjaxResult remove(@PathVariable Long[] districtIds) {
        return toAjax(ywpdGameDistrictService.deleteYwpdGameDistrictByIds(districtIds));
    }
}
